/**
 * weiyinli.com Inc.
 * Copyright (c) 2006-2011 All Rights Reserved.
 */
package com.nmp.web.controller.weibo;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.nmp.service.base.result.Result;
import com.nmp.service.base.result.ResultCode;
import com.nmp.service.weibo.WeiboAccountService;
import com.nmp.web.controller.base.ControllerSupport;

/**
 * 微博帐户管理，包括新浪、腾讯等
 * @author defier.lai
 * 2011-11-7 上午12:56:20
 * @version 1.0
 */
@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@RequestMapping("/weibo")
public class WeiboAccountController extends ControllerSupport {
	
	private final static Logger LOG	= LoggerFactory.getLogger(WeiboAccountController.class);
	
	@Resource
	private WeiboAccountService weiboAccountService;
	
	@RequestMapping(value="/account/index")
    public ModelAndView index(@RequestParam(defaultValue="0") int siteType, @RequestParam(defaultValue="1")  int currentPage) {
		try {
			Result result = weiboAccountService.findWeiboAccountBySiteType(siteType, pageSize, currentPage);
			if(result.getResultCode().getCode() == ResultCode.SUCCESS) {
				setPopSuccessMessage(result.getResultCode().getMessage());
				addObject("page", result.getDefaultModel());
				
			} else {
				setPopFailMessage(result.getResultCode().getMessage());
			}
		} catch (Exception e) {
			LOG.error("weibo user index page is error!", e);
		}
    	return getModelAndView("index");  
    }
    
	@RequestMapping(value="/user/connect")
    public void connect(HttpServletRequest request,HttpServletResponse response) throws IOException {  
		String site = request.getParameter("site");
		Result result = weiboAccountService.getAccountAuthorizeUrl(Integer.parseInt(site));
		if(result.getResultCode().getCode() == ResultCode.SUCCESS) {
			String authorizeUrl = (String)result.getDefaultModel();
			response.sendRedirect(authorizeUrl);
		} else {
			LOG.error("connect is error!code:{}, message:{}", result.getResultCode().getCode(), result.getResultCode().getMessage());
		}
    }
	
	/**
	 * 新浪微博绑定回调地址
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/user/callback/sina")
    public ModelAndView callbackSina(@RequestParam("code") String code) {
		Result result = weiboAccountService.bindSinaWeiboToken(code);
		if(result.getResultCode().getCode() == ResultCode.SUCCESS) {
			getPopMessage().setStatus(true);
			getPopMessage().setMessage("绑定新浪微博帐户成功！");
		} else {
			getPopMessage().setStatus(false);
			getPopMessage().setMessage("绑定新浪微博帐户失败！");
		}
    	return getModelAndView("callback");
    }
	
	
	@RequestMapping(value="/user/callback/tencent")
    public ModelAndView callbackTencent(@RequestParam("oauth_token") String oauthToken, @RequestParam("oauth_verifier") String verifier) {
		Result result = weiboAccountService.bindTencentWeiboToken(oauthToken, verifier);
		if(result.getResultCode().getCode() == ResultCode.SUCCESS) {
			getPopMessage().setStatus(true);
			getPopMessage().setMessage("绑定新浪微博帐户成功！");
		} else {
			getPopMessage().setStatus(false);
			getPopMessage().setMessage("绑定新浪微博帐户失败！");
		}
		return getModelAndView("callback");
	}
    
	@Override
	public String getParentPath() {
		return "/weibo/account";
	}
	
	

}
